<?php
include_once './config/config.php';
$LoginAdmin = auth();
if ($_POST) {
  // 接收参数
  $username = $_POST['username'];
  $password = $_POST['password'];
  $repass = $_POST['repass'];

  if (!empty($_POST['checkUsername'])) {
    $checkUsername = $_POST['checkUsername'];
    $sql = "SELECT * FROM `stu_admin` WHERE username='$checkUsername'";
    $res = mysqli_query($conn, $sql);
    if ($res && mysqli_num_rows($res) > 0) {
      echo json_encode(['msg' => '用户名已存在', 'code' => 0, 'data' => null]);
      exit;
    } else {
      echo json_encode(['msg' => '用户名可以使用', 'code' => 1, 'data' => null]);
      exit;
    }
  }
  // 生成密码盐
  $salt = build_ranstr();

  // 封装数组
  $data = [
    'username' => $username,
    'password' => md5($password.$salt),
    'salt' => $salt
  ];

  $avatar = upload_img('avatar');

  if($avatar['code'] == 0)
  {
    ShowMsg($avatar['msg']);
  }

  $data['avatar'] = $avatar['data'];

  $result = add('admin',$data);

  if($result)
  {
    ShowMsg('新增管理员成功','adminlist.php');
  }else{
    ShowMsg('新增管理员失败');
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once './common/meta.php'; ?>
  <style>
    input[type="file"] {
      display: none;
    }

    label[for="avatar"] {
      width: 200px;
    }
  </style>
</head>

<body>
  <!--<![endif]-->

  <?php include_once './common/header.php' ?>

  <?php include_once './common/nav.php' ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加管理员</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li><a href="adminlist.php">adminList</a> <span class="divider">/</span></li>
      <li class="active">adminAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='adminlist.php'"><i class="icon-list"></i> 管理员列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">

                <label>用户名</label>
                <input type="text" name="username" required class="input-xxlarge" placeholder="请输入用户名">
                <span class="msg"></span>

                <label>密码</label>
                <input type="password" name="password" required class="input-xxlarge" placeholder="请输入密码">

                <label>确认密码</label>
                <input type="password" name="repass" required class="input-xxlarge" placeholder="请输入确认密码">
                <span class="msg2"></span>

                <label>头像</label>
                <label for="avatar">
                  <img src="assets/images/upload.png" id="upload" alt="">
                </label>
                <input type="file" name="avatar" id="avatar" class="input-xxlarge">

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>

  <?php include_once './common/script.php' ?>


</body>

</html>
<script>
  $('#avatar').change(function() {
    // console.log($(this));
    // console.log(this.files[0]);
    // 获取当前的文件
    var file = this.files[0] || {}

    // 返回对象地址
    var url = GetObjectURL(file)

    // // 把图像更新
    $('#upload').attr('src', url)
  })
  $("[name='username']").change(function() {
    $.ajax({
      url: 'adminadd.php',
      type: 'post',
      dataType: 'json',
      data: {
        checkUsername: $(this).val()
      },
      success(res) {
        if (res.code) {
          $('.msg').css({
            color: 'green'
          }).html(res.msg)
        } else {
          $('.msg').css({
            color: 'red'
          }).html(res.msg)
        }
      },
      erorr(err) {
        console.log('err', err);
      }
    })
  })
  $("[name='repass']").change(function() {
    if ($("[name='password']").val() != $(this).val()) {
      $('.msg2').css({
        color: 'red'
      }).html('密码不一致')
    }else{
      $('.msg2').html('')
    }
  })
  // $('[name="username"]').change(function() {
  //   $.post("adminadd.php", {
  //     username: $(this).val(),
  //     action: 'usernameajax',
  //   }, function(res) {
  //     // console.log(res);
  //     if (res.code) {
  //       $('.msg').css({
  //         color: 'green'
  //       }).html(res.msg)
  //     } else {
  //       $('.msg').css({
  //         color: 'red'
  //       }).html(res.msg)
  //     }
  //   }, "json")
  // })
</script>